package jnBase
{
	import jnBase.components.GetRequest;
	
	import laya.events.Event;
	import laya.events.EventDispatcher;
	import laya.net.HttpRequest;
	import laya.utils.Browser;
	
	public class Api extends EventDispatcher{
		public function Api():void
		{
			super()
		}
		/**
		 * 获取token用户信息 ok
		 */
		public static function GetTokenUser(caller:*=null,handler:Function=null):void{
			var params:Object={};
			params.url=Data.HttpRequest+"/User/GetTokenUser";
			Api.getRequest(params,caller,handler)
		}
		/**
		 * 重新开始实验 /api/Experiment/Restart ok 
		 */
		public static function Restart(practiceid:String,experimentid:String,caller:*=null,handler:Function=null):void{
			var params:Object={};
			params.data={experimentid:experimentid,pricticeid:practiceid};
			params.url=Data.HttpRequest+"/Experiment/Restart";
			Api.getRequest(params,caller,handler)
		}
		public static function GetUserInfo(userid:String,caller:*=null,handler:Function=null):void{
			var params:Object={};
			params.data={userid:userid}
			params.url=Data.HttpRequest+"/User/GetUserInfo";
			Api.getRequest(params,caller,handler)
		}
		/**
		 * 保存临时值 ok
		 * @param type  0:考核 1:练习
		 * @param id 练习id 或者 考核id
		 * @param tempvalue 要保存的值
		 * @param caller,handler 回调函数
		 * 
		 */		
		public static function SaveTempValue(type,id,tempvalue:String,caller:*=null,handler:Function=null):void
		{
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/SaveTempValue";
			var data:Object={};
			if(type!=0){
				data.practiceid=id;
			}else data.checkrecordid=id;
			data.tempvalue=tempvalue;
			params.data=data;
			Api.postRequest(params,caller,handler);
		}
		/**
		 * 获取练习记录信息 ok
		 * @param type  0:考核 1:练习 2:任务
		 * @param practiceid 练习记录
		 * @param caller,handler 回调函数
		 * 
		 */		
		public static function GetPracticeById(type:int,id:String,caller:*=null,handler:Function=null):void
		{
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/GetPracticeById";
			var data:Object={};
			if(type!=0){
				data.practiceid=id;
			}else data.checkrecordid=id;
			params.data=data;
			Api.getRequest(params,caller,handler)
		}
		/**
		 * 实验提交操作步骤记录 ok
		 * @param type 0:考核 1:练习
		 * @param practiceid 练习id
		 * @param stepid 步骤
		 * @param usedtime 累计用时
		 * @param isstart 是否开始1开始 0结束 
		 * @param examduration 步骤用时
		 * @param caller,handler 回调函数
		 * 
		 */		
		public static function AddStep(type:int,id:String,stepid:String,usedtime:Number,isstart:Number=1,examduration:Number=0,caller:*=null,handler:Function=null):void
		{
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/AddStep";
			var data:Object={};
			if(type!=0){
				data.practiceid=id;
			}else data.checkrecordid=id;
			data.stepid=stepid;
			data.usedtime=usedtime;
			data.isstart=isstart;
			data.examduration=examduration;
			params.data=data;
//			params.data="practiceid="+id+"&stepid="+stepid+"&usedtime="+usedtime+"&isstart="+isstart+"&examduration="+examduration+"&checkrecordid=''";
			Api.postRequest(params,caller,handler)
		}
		/**
		 * 保存实验报告 ok
		 * @param type  0:考核  1:练习
		 * @param id  id
		 * @param checkReportValues 报告内容
		 * @param caller,handler 回调函数
		 * 
		 */		
		public static function SaveMedicineExperiment(type:int,id:String,checkReportValues:Array,caller:*=null,handler:Function=null):void
		{
			var data:Object={};
			data.checkReportType=type;
			data.stuCheckRecordId=id;
			data.checkReportValues=checkReportValues;
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/SaveMedicineExperiment";
			params.data=data;
			Api.postRequest(params,caller,handler)
		}
		/**
		 * 获取实验报告 ok
		 * @param type  0:考核 1:练习
		 * @param id 练习id/考核id
		 * @param caller,handler
		 * 
		 */		
		public static function GetReportByStuCheckRecordId(type:int,id:String,caller:*=null,handler:Function=null):void
		{
			var data:Object={};
			if(type!=0){
				data.practiceid=id;
			}else data.checkrecordid=id;
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/GetReportByStuCheckRecordId";
			params.data=data;
			Api.getRequest(params,caller,handler)
		}
		/**
		 *实验完成更改状态  ok
		 * @param type  0:考核 1:练习 2:任务
		 * @param id 练习id/考核id
		 * @param usedtime 累计用时
		 * @param caller,handler
		 * 
		 */		
		public static function SaveExperiment(type:int,id:String,usedtime:Number,caller:*=null,handler:Function=null):void
		{
			var data:Object={};
			if(type!=0){
				data.practiceid=id;
			}else data.checkrecordid=id;
			data.usedtime=usedtime
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/SaveExperiment";
			params.data=data;
			Api.postRequest(params,caller,handler)
		}
		/**
		 *	获取实验操作考核记录:ok
		 * @param operatecheckid 考核id
		 * @param caller,handler
		 * 
		 */		
		public static function AddExperimentPractice(operatecheckid:String,caller:*=null,handler:Function=null):void
		{
			var data:Object={};
			data.operatecheckid=operatecheckid;
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/AddExperimentPractice";
			params.data=data;
			Api.getRequest(params,caller,handler)
		}
		/**
		 * 获取实验操作任务的 练习记录id
		 */
		public static function GetOperateTsakPracticeId(taskid:String,experimentId:String,caller:*=null,handler:Function=null):void
		{
			var data:Object={};
			data.pubtaskid=taskid;
			data.experimentid=experimentId;
			var params:Object={};
			params.url=Data.HttpRequest+"/Experiment/GetOperateTsakPracticeId";
			params.data=data;
			Api.getRequest(params,caller,handler)
		}
		/**
		 * 上传图片 ok
		 */
		public static function UplodeFile(file:*,caller:*=null,okhandler:Function=null,error:Function=null):void
		{
			var params:Object={};
			params.url=Data.HttpRequest+"/WebUploader/UplodeFile";
			params.data=file;
			var xhr:HttpRequest=new HttpRequest()
			xhr.http.timeout=10000;
			xhr.send(params.url,params.data,'post','json',['Authorization','Bearer '+GetRequest.getRequests().token]);
			if(okhandler) xhr.once(Event.COMPLETE,caller,okhandler);
			if(error) xhr.on(Event.ERROR,caller,error)
		}
		/**
		 * 用地址获取base64文件 ok
		 */
		public static function GetFileBase64(url:String,caller:*=null,okhandler:Function=null):void
		{
			var params:Object={};
			params.url=Data.HttpRequest+"/WebUploader/GetFileBase64";
			params.data={fileurl:url};
			Api.getRequest(params,caller,okhandler);
		}
		
		private static function getRequest(params,caller:*=null,handler:Function=null):void{
			params.method='get';
			params.type='json';
			Api.request(params,caller,handler)
		}
		private static function postRequest(params,caller:*=null,handler:Function=null):void{
			params.method='post';
			params.type='json';
			Api.request(params,caller,handler)
		}
		private static function request(parameters:Object,caller:*=null,handler:Function=null):void
		{
			var xhr:HttpRequest=new HttpRequest()
			xhr.http.timeout=10000;
			if(parameters.method=='get'){
				if(parameters.data){
					parameters.url+="?"+getParams(parameters.data)
				}
				xhr.send(parameters.url,'',parameters.method,parameters.type,['Authorization','Bearer '+GetRequest.getRequests().token]);
			}
			else{
				xhr.send(parameters.url,JSON.stringify(parameters.data),parameters.method,parameters.type,['Authorization','Bearer '+GetRequest.getRequests().token,"Content-Type", "application/json"]);
			}
			if(handler) xhr.once(Event.COMPLETE,caller,handler)
		}
		private static function getParams(obj:Object):String{
			trace('getParams',obj)
			var names:Array=[];
			names=Browser.window.Object.getOwnPropertyNames(obj);
			var str:String='';
			var i:int=0;
			for each (var value:String in obj) 
			{
				str+=names[i]+'='+value+'&'	;
				i++;
			}
			str=str.substring(0,str.length-1)
			return str
		}
		
		private static function errorHandler(e:Object):void
		{
			// TODO Auto Generated method stub
			trace(e)
		}
	}
	
}